package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.SmsFlashPromotionProductRelation;
import com.itheima.domain.SmsFlashPromotionSession;
import com.itheima.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.mapper.SmsFlashPromotionSessionMapper;
import com.itheima.service.db.SmsFlashPromotionSessionService;
import com.itheima.util.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ChenTianSui
 * @date 2022/11/12 10:39
 */
@DubboService
public class SmsFlashPromotionSessionServiceImpl implements SmsFlashPromotionSessionService {
    @Autowired
    private SmsFlashPromotionSessionMapper sessionMapper;
    @Autowired
    private SmsFlashPromotionProductRelationMapper productRelationMapper;

    //获取所有秒杀场次信息
    @Override
    public List<SmsFlashPromotionSession> findList() {
        LambdaQueryWrapper<SmsFlashPromotionSession> qw = new LambdaQueryWrapper<>();
        return sessionMapper.selectList(qw);

    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        SmsFlashPromotionSession smsFlashPromotionSession = sessionMapper.selectById(id);
        if (smsFlashPromotionSession != null && status != null) {
            smsFlashPromotionSession.setStatus(status);

        }
        sessionMapper.updateById(smsFlashPromotionSession);
    }

    @Override
    public R create(SmsFlashPromotionSession smsFlashPromotionSession) {
        int insert = sessionMapper.insert(smsFlashPromotionSession);
        return R.success(insert);
    }

    @Override
    public R delete(Integer id) {
        return R.success(sessionMapper.deleteById(id));

    }

    @Override
    public Integer update(SmsFlashPromotionSession smsFlashPromotionSession) {
        return sessionMapper.updateById(smsFlashPromotionSession);

    }

    @Override
    public SmsFlashPromotionSession findById(Integer id) {
        return sessionMapper.selectById(id);

    }

    @Override
    public List<Map<String, Object>> selectList(Integer flashPromotionId) {

        /*List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelations = productRelationMapper.selectList(qw);
        if (CollUtil.isNotEmpty(smsFlashPromotionProductRelations)) {
            smsFlashPromotionProductRelations.stream().map(
                    e -> {
                        SmsFlashPromotionSession smsFlashPromotionSession = sessionMapper.selectById(e.getFlashPromotionSessionId());
                        productRelationMapper.selectById(e.getProductId());


                    }
            ).collect(Collectors.toList());*/

        //获取所有秒杀场次信息
        List<SmsFlashPromotionSession> list = findList();
        List<Map<String, Object>> selectList = new ArrayList<>();
        //遍历list
        if (CollUtil.isNotEmpty(list)) {
            selectList = list.stream().map(
                    e -> {
                        LambdaQueryWrapper<SmsFlashPromotionProductRelation> qw = new LambdaQueryWrapper<>();
                        qw.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
                        qw.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, e.getId());
                        Integer productCount = productRelationMapper.selectCount(qw);
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", e.getId());
                        map.put("name", e.getName());
                        map.put("productCount", productCount);
                        map.put("startTime", e.getStartTime());
                        map.put("status", e.getStatus());
                        map.put("createTime", e.getCreateTime());
                        map.put("endTime", e.getEndTime());
                        return map;
                    }
            ).collect(Collectors.toList());

        }
        return selectList;
    }

}


 
